Page({
  data: {
    name: '',
    gender: '男',
    age: '',
    height: '',
    weight: '',
    relationship: '',
    relationships: ['父亲', '母亲', '儿子', '女儿', '爷爷', '奶奶', '外公', '外婆', '其他'],
    isSubmitting: false
  },

  onLoad: function (options) {
    // 页面加载时的初始化逻辑
  },

  onNameInput: function(e) {
    this.setData({
      name: e.detail.value
    });
  },

  selectGender: function(e) {
    const gender = e.currentTarget.dataset.gender;
    this.setData({
      gender: gender
    });
  },

  onAgeInput: function(e) {
    this.setData({
      age: e.detail.value
    });
  },

  onHeightInput: function(e) {
    this.setData({
      height: e.detail.value
    });
  },

  onWeightInput: function(e) {
    this.setData({
      weight: e.detail.value
    });
  },

  onRelationshipChange: function(e) {
    const index = e.detail.value;
    this.setData({
      relationship: this.data.relationships[index]
    });
  },

  submitMember: function() {
    // 表单验证
    if (!this.data.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      });
      return;
    }

    if (!this.data.age) {
      wx.showToast({
        title: '请输入年龄',
        icon: 'none'
      });
      return;
    }

    // 防止重复提交
    if (this.data.isSubmitting) {
      return;
    }

    this.setData({
      isSubmitting: true
    });

    // 构建成员数据
    const memberData = {
      name: this.data.name,
      gender: this.data.gender === '男' ? 'male' : 'female',
      age: parseInt(this.data.age),
      height: this.data.height ? parseFloat(this.data.height) : null,
      weight: this.data.weight ? parseFloat(this.data.weight) : null,
      relationship: this.data.relationship || '其他'
    };

    // 获取token
    const token = wx.getStorageSync('token');
    if (!token) {
      wx.showToast({
        title: '登录状态失效，请重新登录',
        icon: 'none'
      });
      this.setData({ isSubmitting: false });
      return;
    }

    // 发送真实API请求添加成员
    console.log('准备发送的成员数据:', memberData);
    
    wx.request({
      url: 'http://localhost:3000/api/members',
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${token}`
      },
      data: memberData,
      success: (res) => {
        console.log('服务器响应:', res.data);
        if (res.data.success) {
          wx.showToast({
            title: '添加成功',
            icon: 'success'
          });
          
          // 延迟返回上一页
          setTimeout(() => {
            // 返回并刷新上一页
            const pages = getCurrentPages();
            const prevPage = pages[pages.length - 2]; // 获取上一个页面
            
            // 如果上一页是首页，触发刷新
            if (prevPage && prevPage.route === 'pages/index/index') {
              prevPage.loadFamilyMembers();
            }
            
            wx.navigateBack();
          }, 1000);
        } else {
          wx.showToast({
            title: res.data.message || '添加失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
        wx.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({
          isSubmitting: false
        });
      }
    });
  },

  cancel: function() {
    wx.navigateBack();
  }
}) 